using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BatchBuild2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*
            comboBox1.Text = @"D:\Documents\My Projects\APICTA2008";
            DirectoryInfo dirApplication = new DirectoryInfo(comboBox1.Text);
            ProduceListing(dirApplication);
            */
            
            toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;

            toolTip1.SetToolTip(comboMadPath,"Specify the location of the madbuild compiler.");

            button1.Enabled = false;
            button3.Enabled = false;

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DialogResult OPEN = folderBrowserDialog1.ShowDialog();

            checkBox1.Checked = false;
            if (OPEN == DialogResult.OK)
            {

                comboBox1.Text = folderBrowserDialog1.SelectedPath;
                
                DirectoryInfo dirApplication = new DirectoryInfo(comboBox1.Text);
                checkedListBox1.Items.Clear();
                checkedListBox2.Items.Clear();
         
                ProduceListing(dirApplication);
                checkBox1.Checked = true;
                checkBox1.Click += new System.EventHandler(checkBox1_CheckedChanged);
                toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;
            }

            
        }

        private void ProduceListing(DirectoryInfo dinfo)
        {

            FileInfo[] filApplication;
            FileInfo[] filFlareProject;
            filFlareProject =dinfo.GetFiles("*.*prj");

 
            foreach (FileInfo filInfo in filFlareProject)
            {
                checkedListBox1.Items.Add(filInfo);
                toolStripProgressBar1.PerformStep();
                Application.DoEvents();
            }

            foreach (DirectoryInfo subdir in dinfo.GetDirectories())
            {


                filApplication = subdir.GetFiles("*.fltar");
                foreach (FileInfo filInfo in filApplication)
                {
                    //checkedListBox2.Items.Add(filInfo.Name.Remove(filInfo.Name.Length-6));
                    
                }

                if (subdir.GetDirectories().Length > 0)
                {
                    ProduceListing(subdir);
                }
 

            }
            Application.DoEvents();
            toolStripProgressBar1.PerformStep();

        }
        private void produceTargetListings(DirectoryInfo dinfo)
        {
            FileInfo[] filTargets;
            filTargets = dinfo.GetFiles("*.fltar");

            foreach (FileInfo filInfo in filTargets)
            {
                //checkedListBox2.Items.Add("Hello World");
                checkedListBox2.Items.Add(filInfo.Name);
            }

            foreach (DirectoryInfo subdir in dinfo.GetDirectories())
            {
                filTargets = subdir.GetFiles("*.fltar");
                foreach (FileInfo filInfo in filTargets)
                {
                    //checkedListBox2.Items.Add("Hello World");
                    checkedListBox2.Items.Add(Path.GetFileNameWithoutExtension(filInfo.Name));
                }

                if (subdir.GetDirectories().Length > 0)
                {
                    produceTargetListings(subdir);
                }
            }

        }

        private void makeBatch()
        {
            string pathToProject="";
            string theTarget = "";
            string body = "\r\n";
            string selectProjectTarget = "\r\n";

            if (this.checkedListBox1.CheckedItems.Count > 0)
            {
                foreach (FileInfo myitem in checkedListBox1.CheckedItems)
                {
                    if (this.checkedListBox2.CheckedItems.Count > 0)
                    {
                        foreach (string tgt in checkedListBox2.CheckedItems)
                        {
                            selectProjectTarget = selectProjectTarget + getBuildCommand(myitem.FullName,tgt);
                        
                        }
                    }
                    else
                    {
                        selectProjectTarget = selectProjectTarget + getBuildCommand(myitem.FullName);
                    }
                
                }
            }


            body = body + selectProjectTarget;


            textBoxBatchPreview.Text = getHeader() + body;

            if (textBoxBatchPreview.Text.ToString() != null)
            {
                button1.Enabled = true;
                button3.Enabled = true;
            }

        }

        private string getHeader()
        {
            string path = "set path=%path%;" + comboMadPath.Text;
            string header = path;
            return header;
        }

        private string getBuildCommand(string theProject, string theTarget)
        {
            string buildCommand;
            buildCommand = "madbuild -project " + theProject + " -target " + theTarget + "\r\n";
            
            return buildCommand;

        }
        
        private string getBuildCommand(string theProject)
        {
            string buildCommand;
            buildCommand = "madbuild -project \"" + theProject + "\"\r\n";

            return buildCommand;

        }


        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a;
            checkedListBox2.Items.Clear();
            a= checkedListBox1.CheckedItems.Count.ToString() + " project(s) selected.";
            DirectoryInfo dinfo;
            foreach (FileInfo myitem in checkedListBox1.SelectedItems)
            {
                string fNamePath = myitem.FullName.ToString();
                string fPathOnly = Path.GetDirectoryName(fNamePath);
                dinfo = new DirectoryInfo(fPathOnly);
                produceTargetListings(dinfo);
                toolTip1.SetToolTip(checkedListBox1, fNamePath);
            }
            toolStripStatusLabel2.Text = a;
       
            makeBatch();
        }




        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void comboMadPath_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(textBoxBatchPreview.Text);
            }
            catch (System.ArgumentNullException)
            {
                toolStripStatusLabel2.Text = "Nothing to copy to Clipboard.";
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    checkedListBox1.SetItemChecked(i,true);

                }
                toolStripStatusLabel2.Text = checkedListBox1.Items.Count + " project(s) selected.";


            }

            else
            {
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    checkedListBox1.SetItemChecked(i, false);
                    /*if (checkedListBox2.Items.Count>0)
                    {
                        checkedListBox2.SetItemChecked(i, false);
                    }*/

                }
                textBoxBatchPreview.Text = "";
                toolStripStatusLabel2.Text = checkedListBox1.Items.Count + " project(s) deselected.";
            }
            makeBatch();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DialogResult SAVE = saveFileDialog1.ShowDialog();

            if (SAVE == DialogResult.OK)
            {
                string a = saveFileDialog1.FileName;

                StreamWriter sw = new StreamWriter(a);
                sw.Write(textBoxBatchPreview.Text);
                sw.Close();

                toolStripStatusLabel2.Text = "Saved to " + a;
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {

        }

        private void textBoxBatchPreview_TextChanged(object sender, EventArgs e)
        {
            string a = textBoxBatchPreview.Text;
            if (a.Length == 0)
            {
                button1.Enabled = button3.Enabled = false;
            }
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            makeBatch();
        }




    }
}